package com.cutefool.zero.code.admin.company.domain;

import com.cutefool.dsl.mybatis.core.objects.BasicsDomain;
import com.fasterxml.jackson.annotation.JsonAlias;
import jakarta.annotation.Generated;
import java.sql.JDBCType;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.UnaryOperator;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateModel;

@Data
@EqualsAndHashCode(callSuper = true)
public class Company extends BasicsDomain<Company> {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("NAME_")
    private String name;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("EXPIRE_AT")
    private LocalDateTime expireAt;

    private static final Company instance = new Company();

    private static final Table table = new Table();

    public static Company instance() {
        return instance;
    }

    @Override
    public Table table() {
        return table;
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<InsertDSL<Company>> insert() {
        return (InsertDSL<Company> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.name).toProperty("name");
            dsl.map(table.expireAt).toProperty("expireAt");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            return dsl;
        };
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<MultiRowInsertDSL<Company>> insertMultiple() {
        return (MultiRowInsertDSL<Company> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.name).toProperty("name");
            dsl.map(table.expireAt).toProperty("expireAt");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            return dsl;
        };
    }

    @Override
    public void updateAllColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.name).equalTo(this::getName)
           .set(table.expireAt).equalTo(this::getExpireAt)
           .set(table.createBy).equalTo(this::getCreateBy)
           .set(table.createAt).equalTo(this::getCreateAt)
           .set(table.updateBy).equalTo(this::getUpdateBy)
           .set(table.updateAt).equalTo(this::getUpdateAt);
        dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
    }

    @Override
    public void updateSelectiveColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.name).equalToWhenPresent(this::getName)
           .set(table.expireAt).equalToWhenPresent(this::getExpireAt)
           .set(table.createBy).equalToWhenPresent(this::getCreateBy)
           .set(table.createAt).equalToWhenPresent(this::getCreateAt)
           .set(table.updateBy).equalToWhenPresent(this::getUpdateBy)
           .set(table.updateAt).equalToWhenPresent(this::getUpdateAt);
        dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
    }

    @Override
    @SuppressWarnings("all")
    public SqlColumn[] columns() {
        return new SqlColumn[]{table.id, table.name, table.expireAt, table.createBy, table.createAt, table.updateBy, table.updateAt};
    }

    @Override
    public Map<String, SqlColumn<?>> columnsMap() {
        Map<String, SqlColumn<?>> maps = new LinkedHashMap<>(7);
        maps.put("id", table.id);
        maps.put("name", table.name);
        maps.put("expireAt", table.expireAt);
        maps.put("createBy", table.createBy);
        maps.put("createAt", table.createAt);
        maps.put("updateBy", table.updateBy);
        maps.put("updateAt", table.updateAt);
        return maps;
    }

    @Override
    public SqlColumn<Long> id() {
        return table.id;
    }

    public static final class Table extends org.mybatis.dynamic.sql.AliasableSqlTable<Table> {
        public final SqlColumn<Long> id = column("ID_", JDBCType.BIGINT);

        public final SqlColumn<String> name = column("NAME_", JDBCType.VARCHAR);

        public final SqlColumn<LocalDateTime> expireAt = column("EXPIRE_AT", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> createBy = column("CREATE_BY", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> createAt = column("CREATE_AT", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> updateBy = column("UPDATE_BY", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> updateAt = column("UPDATE_AT", JDBCType.TIMESTAMP);

        public Table() {
            super("CUTE_COMPANY", Table::new);
        }
    }
}